//走路 变为通道
function change_space(x, y) {
    $("#area_" + x + "_" + y).css("background-image", "")
    $("#area_" + x + "_" + y).removeClass(["hero", "hero_down", "hero_up", "hero_left", "hero_right","monster"])
    $("#area_" + x + "_" + y).addClass("space")
    map[y][x] = 0
}

//走路 玩家变为通道
function change_stair(x, y) {
    $("#area_" + x + "_" + y).css("background-image", "")
    $("#area_" + x + "_" + y).removeClass(["hero", "hero_down", "hero_up", "hero_left", "hero_right","monster"])
    $("#area_" + x + "_" + y).addClass("door")
    map[y][x] = 2
}

// 走路 变为玩家
function change_player(type, x, y) {
    $("#area_" + x + "_" + y).removeClass("space")
    $("#area_" + x + "_" + y).addClass(["hero", "hero_" + type])
    map[y][x] = 3
}

// 向下事件
function down_event() {
    down = get_down()

    x = parseInt($(".hero").attr("data-x"))
    y = parseInt($(".hero").attr("data-y"))

    direction_event(down, "down", x, y)
}

// 向上事件
function up_event() {
    up = get_up()

    x = parseInt($(".hero").attr("data-x"))
    y = parseInt($(".hero").attr("data-y"))

    direction_event(up, "up", x, y)
}

// 向左事件
function left_event() {
    left = get_left()

    x = parseInt($(".hero").attr("data-x"))
    y = parseInt($(".hero").attr("data-y"))

    direction_event(left, "left", x, y)
}

// 向右事件
function right_event() {
    right = get_right()

    x = parseInt($(".hero").attr("data-x"))
    y = parseInt($(".hero").attr("data-y"))

    direction_event(right, "right", x, y)
}

// 获取向下位置信息
function get_down() {
    // 位置 
    x = parseInt($(".hero").attr("data-x"))
    y = parseInt($(".hero").attr("data-y")) + 1
    attr = map[y][x]
    $(".hero").removeClass(["hero_down", "hero_up", "hero_left", "hero_right"])
    $(".hero").addClass("hero_down")

    return [attr, x, y]
}

// 获取向上位置信息
function get_up() {
    // 位置 
    x = parseInt($(".hero").attr("data-x"))
    y = parseInt($(".hero").attr("data-y")) - 1
    attr = map[y][x]
    $(".hero").removeClass(["hero_down", "hero_up", "hero_left", "hero_right"])
    $(".hero").addClass("hero_up")

    return [attr, x, y]
}

// 获取向左位置信息
function get_left() {
    // 位置 
    x = parseInt($(".hero").attr("data-x")) - 1
    y = parseInt($(".hero").attr("data-y"))
    attr = map[y][x]
    $(".hero").removeClass(["hero_down", "hero_up", "hero_left", "hero_right"])
    $(".hero").addClass("hero_left")

    return [attr, x, y]
}

// 获取向右位置信息
function get_right() {
    // 位置 
    x = parseInt($(".hero").attr("data-x")) + 1
    y = parseInt($(".hero").attr("data-y"))
    attr = map[y][x]
    $(".hero").removeClass(["hero_down", "hero_up", "hero_left", "hero_right"])
    $(".hero").addClass("hero_right")
    
    return [attr, x, y]
}

//战斗日志
function battle_log(content) {
    $(".layui-layer-content").append("<br>" + content)
}

// 刷新战斗信息
function reflush() {
    $(".floor",parent.document).text(floor)
    $(".level",parent.document).text(hero["level"])
    $(".hp",parent.document).text(hero["hp"])
    $(".atk",parent.document).text(hero["atk"])
    $(".def",parent.document).text(hero["defense"])
    $(".hit",parent.document).text(hero["hit"])
    $(".dod",parent.document).text(hero["dod"])
    $(".cri",parent.document).text(hero["cri"])
    $(".exp",parent.document).text(hero["exp"])
    $(".max_exp", parent.document).text(hero["max_exp"])
    $(".skill", parent.document).html("")
    $.each(hero["skill"], function (k, v) {
        $(".skill", parent.document).append(set_skill(v["name"], v["atk"]))
    });
    $.each(hero["equipment"], function (k, v) {
        $(".n" + v["type"], parent.document).text(v["name"])
        $(".i" + v["type"], parent.document).text(v["atk"] == 0 ? v["defense"] : v["atk"])
    });
    
}

// 放置墙
function set_wall(x, y) {
    html = "<div id='area_" + x + "_" + y + "' class='wall' data-x='" + x + "' data-y='" + y + "'></div>";
    return html
}

// 放置通道
function set_space(x, y) {
    html = "<div id='area_" + x + "_" + y + "' class='space' data-x='" + x + "' data-y='" + y + "'></div>";
    return html
}

// 放置门
function set_door(x, y) {
    html = "<div id='area_" + x + "_" + y + "' class='door' data-x='" + x + "' data-y='" + y + "'></div>";
    return html
}

// 放置道具
function set_item(id, x, y) {
    html = "<div id='area_" + x + "_" + y + "' class='item' data-x='" + x + "' data-y='" + y + "' style='background-image: url(/static/images/item/" + id + ".jpg)'></div>"
    return html
}

// 放置怪物
function set_monster(id, x, y) {
    html = "<div id='area_" + x + "_" + y + "' class='monster' data-x='" + x + "' data-y='" + y + "' style='background-image: url(/static/images/monster/" + id + ".jpg)'></div>"
    return html
}

// 放置玩家
function set_hero(type, x, y) {
    html = "<div id='area_" + x + "_" + y + "' class='hero hero_" + type + "' data-x='" + x + "' data-y='" + y + "'></div>";
    return html
}

// 技能html
function set_skill(name, atk) {
    html = '<p class="layui-col-md12">';
    html += '<span class="left"><b>' + name + '</b></span>';
    html += '<span class="right"><b class="skill_1">+' + atk + '</b> ATK</span>';
    html += '</p>';

    return html
}